

import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class MongoService {

    private static MongoClientOptions options;

    private static MongoClient client;

    private static List<Document> cache = new ArrayList<>();

    private static MongoDatabase mongoDatabase;

    private static MongoCollection<Document> mongoCollection;

    public static MongoClient getClient() {
        return client;
    }

    /**
     * 设置连接属性
     */
    static {
        MongoClientOptions.Builder build = new MongoClientOptions.Builder();
        //与数据最大连接数50
        build.connectionsPerHost(1000);
        //如果当前所有的connection都在使用中，则每个connection上可以有50个线程排队等待
        build.threadsAllowedToBlockForConnectionMultiplier(50);
        build.connectTimeout(60 * 1000);
        build.maxWaitTime(2 * 60 * 1000);
        //写入可靠性
        build.writeConcern(WriteConcern.UNACKNOWLEDGED);
        options = build.build();
        System.out.println("属性设置完成");
    }

    /**
     * 开始连接
     */
    public static void connect() {
        String url = "192.168.2.8";
        String dbName = "kafak";
        String collectionName = "test";
        //客户端
        client = new MongoClient(url, options);
        //表
        mongoDatabase = client.getDatabase(dbName);
        //获取某个表
        mongoCollection = mongoDatabase.getCollection(collectionName);
    }

    /**
     * 关闭连接
     */
    public static void close() {
        client.close();
    }

    /**
     * 新增
     *
     * @param document
     */
    private static void add(Document document) {
        //获取数据库
        mongoCollection.insertOne(document);
        //System.out.println("保存成功");
    }

    private static void addBatch(List<Document> documentList) {
        //获取数据库
        MongoDatabase db = client.getDatabase("kafak");
        //获取某个表
        MongoCollection<Document> test = db.getCollection("test");
        test.insertMany(documentList);
        System.out.println("保存成功...");
    }


    /**
     * 添加 加入缓存机制
     *
     * @param jsonString
     */
    //todo 存在BUG
    public static void addAsJSONCache(String jsonString) {
        if (cache.size() >= 100) {
            addBatch(cache);
            cache.clear();
        }
        Document document = new Document();
        document.putAll(JSONObject.parseObject(jsonString));
        cache.add(document);
    }

    /**
     * 添加
     *
     * @param jsonString
     */
    public static void addAsJSON(String jsonString) {
        Document document = new Document();
        document.putAll(JSONObject.parseObject(jsonString));
        add(document);
    }


    @Test
    public void test() {
        MongoService.connect();
        Long timeS = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            Document document = new Document();
//            document.put("_id",i+1);
            document.put("time1", System.currentTimeMillis());
            document.put("time2", "151561561665asdasdasdsa");
            document.put("time3", "asdasdsadadsadadadasdasd");
            document.put("time4", "asdasdasdasdadasdsadad");
            document.put("time5", "asdasdasdasdadasdasdasdsa");
            document.put("time6", "asdasdasdasdsadadadadasd");
            add(document);
        }
        Long timeE = System.currentTimeMillis();
        System.out.println(timeE - timeS);
    }

    public static void getList() {

    }
}